﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace IKEA.FileExchange.Web.Controls
{
    public class VistaButton : WebControl, IPostBackEventHandler
    {
        public bool AppendQueryUrlToHyperLink
        {
            get
            {
                object o = ViewState["AppendSourceUrlToHyperLink"];

                if (o == null)
                {
                    o = false;
                    ViewState.Add("AppendSourceUrlToHyperLink", o);
                }


                return (bool)o;
            }
            set
            {
                ViewState.Add("AppendSourceUrlToHyperLink", value);
            }
        }

        public string HyperLink
        {
            get
            {
                object o = ViewState["HyperLink"];

                if (o == null)
                {
                    ViewState.Add("HyperLink", string.Empty);
                }

                return (string)o;
            }
            set
            {
                ViewState.Add("HyperLink", value);
            }
        }

        public string ButtonText
        {
            get
            {
                object o = ViewState["ButtonText"];

                if (o == null)
                {
                    ViewState.Add("ButtonText", string.Empty);
                }

                return (string)o;
            }
            set
            {
                ViewState.Add("ButtonText", value);
            }
        }

        public string ImageUrl
        {
            get
            {
                object o = ViewState["ImageUrl"];

                if (o == null)
                {
                    ViewState.Add("ImageUrl", string.Empty);
                }

                return (string)o;
            }
            set
            {
                ViewState.Add("ImageUrl", value);
            }
        }

        public event EventHandler Click; 
                                                                                                                                                            
        #region IPostBackEventHandler Members

        public void RaisePostBackEvent(string eventArgument)
        {
            RaiseClickEvent();
        }

        #endregion                                                                                                                                                

        private void RaiseClickEvent()
        {
            if (Click != null)
            {
                Click(this, EventArgs.Empty);
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            string url = Page.ClientScript.GetWebResourceUrl(GetType(), "IKEA.FileExchange.Web.VistaButtonCss.css");

            LiteralControl ltrlCssLink = new LiteralControl("<link rel='stylesheet' href=\"" + url + "\" />");
            Page.Header.Controls.Add(ltrlCssLink);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.AddAttribute("class", "vista_toolbar");
            writer.AddAttribute("cellpadding", "0");
            writer.AddAttribute("cellspacing", "0");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            writer.AddAttribute("class","vista_toolbar_leftborder");
            writer.AddAttribute("valign", "top");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.RenderEndTag();

            writer.AddAttribute("align", "center");
            writer.AddAttribute("class", "vista_toolbar_content");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            writer.AddAttribute("align", "center");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            writer.AddAttribute("align", "center");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            if (!string.IsNullOrEmpty(ImageUrl))
            {
                writer.AddAttribute("src", Page.ResolveClientUrl(ImageUrl));
                writer.RenderBeginTag(HtmlTextWriterTag.Img);
                writer.RenderEndTag();
            }

            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            if (string.IsNullOrEmpty(HyperLink))
            {
                writer.AddAttribute("href", Page.ClientScript.GetPostBackClientHyperlink(this, string.Empty));
            }
            else if (!string.IsNullOrEmpty(HyperLink))
            {
                string url = HyperLink;

                if (AppendQueryUrlToHyperLink)
                {
                    url = string.Concat(url, "?", "source=", Page.Request.Url.PathAndQuery);
                }

                writer.AddAttribute("href", url);
            }
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write(ButtonText);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderEndTag();

            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
        }                                               
    }
}
